/**
 * Created by xiaoz on 16/8/13.
 */
define(["utils","base"],function(utils,baseClass){
    var Component = function(config){
        var _this = this;
        this.pages={};
        Component.baseConstructor.call(this,config);
        this.$el.addClass("yy-pagecontainer");
        this.pageview.innerPageViewContainer = this;
        this.pageview.pagecontainer = this;
        if(this.pageview.innerPageViewContainerNeedTriggerHashChange){
          this.pageview.innerPageViewContainerNeedTriggerHashChange = false;
          this.hashChange(this.pageview.curRoutes);
        }
    }

    utils.extendClass(Component,baseClass);

    Component.prototype.getRoutes =function(){
      return this.routes || this.pageview.routes;
    };
    Component.prototype.hashChange = function(routes){
      var _this =this;
      this.routes = utils.copy(routes);
      var pageKey = routes.shift();

      if(!pageKey){
        pageKey = this.config.defaultPageKey;
      }

      var pageInstance = this.pages[pageKey];

      _this.curPageView&&_this.curPageView.hideCurShowPage();

      if(pageInstance&&pageInstance.config.nocache!==true){
        _this.curPageView && _this.curPageView.$el.addClass("displaynone");
        _this.curPageView = pageInstance;
        pageInstance.removeClass("displaynone");
        var ResumeMethod = pageInstance.plugin["onPageResume"];
        pageInstance.params = pageInstance.pageManager.getParamsFromUrl();
        ResumeMethod&&ResumeMethod.call(pageInstance.plugin,pageInstance,{});
        pageInstance.hashChange(routes);
        return;
      }else{
        if(pageInstance){
          pageInstance.$el.remove();
        }
      }
      if(!pageKey){
          return;
      }
      var pageFileName = pageKey.split("_")[0];
      _this.pageview.pageManager.getPageConfigByPageKey(pageFileName,function(pageConfig){
        pageConfig.pagecontainer = _this;
        pageConfig.fullPageKey = pageKey;
        pageInstance =  _this.pageview.pageManager.createPageView(pageConfig);
        _this.pages[pageKey] = pageInstance;
        pageInstance.hashChange(routes);
        _this.$el.append(pageInstance.$el);
        _this.curPageView && _this.curPageView.$el.addClass("displaynone");
        _this.curPageView  = pageInstance;
      },function(e){
        if(error){
          error(fromPageInstance);
        }
      });
    }

    return Component;
});
